﻿function ConvertTo-UnsecureString(
    [System.Security.SecureString][parameter(mandatory=$true)]$SecurePassword)
{
    $unmanagedString = [System.IntPtr]::Zero;
    try
    {
        $unmanagedString = [Runtime.InteropServices.Marshal]::SecureStringToGlobalAllocUnicode($SecurePassword)
        return [Runtime.InteropServices.Marshal]::PtrToStringUni($unmanagedString)
    }
    finally
    {
        [Runtime.InteropServices.Marshal]::ZeroFreeGlobalAllocUnicode($unmanagedString)
    }
}

function ConvertTo-Base64($string) {
   $bytes  = [System.Text.Encoding]::UTF8.GetBytes($string);
   $encoded = [System.Convert]::ToBase64String($bytes);

   return $encoded;
}

function ConvertFrom-Base64($string) {
   $bytes  = [System.Convert]::FromBase64String($string);
   $decoded = [System.Text.Encoding]::UTF8.GetString($bytes);

   return $decoded;
}

function Get-HttpBasicHeader($Credentials, $Headers = @{})
{
	$b64 = ConvertTo-Base64 "$($Credentials.UserName):$(ConvertTo-UnsecureString $Credentials.Password)"
	$Headers["Authorization"] = "Basic $b64"
	return $Headers
}

function Get-JIRAserverInfo {

param($Credentials = $(Get-Credential), $BaseURI = "http://jira-test6.np.wc1.yellowpages.com:8080")

$uri = "$BaseURI/rest/api/latest/serverInfo"

$headers = Get-HttpBasicHeader $Credentials
Invoke-RestMethod -uri $uri -Headers $headers -ContentType "application/json"
if ($VerbosePreference) {echo "$uri"}
}

function Get-JIRAuser {

param($userID, $Credentials = $(Get-Credential), $BaseURI = "http://jira-test6.np.wc1.yellowpages.com:8080")

$uri = "$BaseURI/rest/api/latest/user?$userID"

$headers = Get-HttpBasicHeader $Credentials
Invoke-RestMethod -uri $uri -Headers $headers -ContentType "application/json"

if ($VerbosePreference) {echo "$uri"}
}

function Get-JIRAdashboard {

param($Credentials = $(Get-Credential), $BaseURI = "http://jira-test6.np.wc1.yellowpages.com:8080")

$uri = "$BaseURI/rest/api/latest/dashboard"

$headers = Get-HttpBasicHeader $Credentials
Invoke-RestMethod -uri $uri -Headers $headers -ContentType "application/json"

if ($VerbosePreference) {echo "$uri"}
}

function Get-JIRAissuetype {

param($Credentials = $(Get-Credential), $BaseURI = "http://jira-test6.np.wc1.yellowpages.com:8080")

$uri = "$BaseURI/rest/api/latest/issuetype"

$headers = Get-HttpBasicHeader $Credentials
Invoke-RestMethod -uri $uri -Headers $headers -ContentType "application/json"

if ($VerbosePreference) {echo "$uri"}
}

function Get-JIRAissue {

[CmdletBinding()]
param(
    $Issue, 
    $Credentials = $(Get-Credential), 
    $BaseURI = "http://jira-test6.np.wc1.yellowpages.com:8080", 
    [switch]$comment, 
    $commentID, 
    [switch]$editmeta, 
    [switch]$remotelink, 
    [switch]$transitions, 
    [switch]$votes, 
    [switch]$watchers,
    [switch]$worklog,
    $worklogID,
    [switch]$createmeta,
    [switch]$properties,
    $propertyKey
    )

$uri = "$BaseURI/rest/api/latest/issue/$Issue"

if ($comment) {
    $uri = $uri + "/comment"
    if ($commentID) {$uri = $uri + "/" + $commentID}
}

if ($editmeta) {$uri = $uri + "/editmeta"}

if ($remotelink) {$uri = $uri + "/remotelink"}

if ($transitions) {$uri = $uri + "/transitions"}

if ($votes) {$uri = $uri + "/votes"}

if ($watchers) {$uri = $uri + "/watchers"}

if ($worklog) {
    $uri = $uri + "/worklog"
    if ($worklogID) {$uri = $uri + "/" + $worklogID}
}

if ($createmeta) {$uri = $uri + "/createmeta"}

if ($properties) {
    $uri = $uri + "/properties"
    if ($propertyKey) {$uri = $uri + "/" + $propertyKey}
}

$headers = Get-HttpBasicHeader $Credentials
Invoke-RestMethod -uri $uri -Headers $headers -ContentType "application/json"
Write-Verbose "URI is $uri"
}